package com.xian.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 自动提交offset
 * 自动提交offset的相关参数：
 * enable.auto.commit：是否开启自动提交offset功能
 * auto.commit.interval.ms：自动提交offset的时间间隔
 */
public class Consumer2 {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers","r73:9092");
        props.put("group.id","test-group");
        props.put("enable.auto.commit", "false"); //是否启动自动提交offsert
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        
        consumer.subscribe(Arrays.asList("jemson-topic"));  //订阅topic  可以订阅多个
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));//拉取数据   没有数据时间隔一段时间继续拉取
            for (ConsumerRecord<String, String> record : records) {
                String key = record.key();
                String value = record.value();
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
            }


        }
        
        
        
        


    }
}
